<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import defaultImg from 'assets/images/midea_logo.png';
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import route from "constants/route";

    export default{
        data(){
            return{
                defaultImg: defaultImg,
                subject: null, //搜素关键字
                shopType: null, //大店铺关键key
                sid: null, //小店铺关键key
                order: 1, //默认价格排序

                goodsList:[],
                shopList: [],
                hasSearchGoods: true, //搜索有无商品标识

                /**分页参数**/
                start: 0,
                limit: 9,
                loading: false, //正在加载
                listLoaded: false, //默认的页面没有加载
                listCompleted: false//分页完成
            }
        },
        created() {
            let self = this;
            let config = weex.config;

            self.subject = config.subject || null; //搜索关键字
            self.shopType = config.shopType || null; //搜素大店铺key
            self.sid = config.sid || null; //搜索小店铺key

            self.sendHttp(res=>{
                self.shopList = res.data.shop || [];
                self.pageInit(res);

                if(!self.goodsList.length) { //初始化判断有无商品=>是否展示排序按钮
                    self.hasSearchGoods = false;
                }
            });
        },
        methods: {
            sendHttp(succCallback,failCallback){
                let self = this;
                let _options = {
                    subject: self.subject,
                    order: self.order,
                    start: self.start,
                    limit: self.limit
                };

                if(self.shopType) _options.shoptype = self.shopType;
                if(self.sid) _options.sid = self.sid;

                api.sendHttp("getSearchResultApi",_options).then(res =>{
                    if(typeof succCallback == "function") succCallback(res)
                },
                res =>{
                    modal.toast(res.msg);
                    if(typeof failCallback == "function") failCallback(res)
                });
            },
            fetchMore(){
                let self = this;
                if(self.listCompleted || self.loading) {
                    return false;
                }

                self.loading = true;
                let temp = self.start + self.limit;
                self.start = temp;

                self.sendHttp(res=>{
                    self.loading = false;
                    let _goodsList = res.data.goods || [];

                    if(_goodsList.length < self.limit) self.listCompleted = true;
                    self.goodsList = self.goodsList.concat(_goodsList);
                },res=>{
                    self.loading = false;
                });
            },
            initData(){
                let self = this;
                self.goodsList.length = 0;
                self.start = 0;
                self.listCompleted = false;
                self.listLoaded = false;
            },
            sortClick(val){
                let self = this;
                    //重置参数
                    switch(val) {
                        case 1:
                            self.order=1;
                            break;
                        case 2:
                            self.order=2;
                            break;
                        case 3:
                            self.order=3;
                            break;
                        case 4:
                            self.order=4;
                            break;
                        default:
                            self.order=1;
                            break;
                    }
                    self.initData();
                    self.sendHttp(res=>{
                        self.pageInit(res);
                    });
            },
            pageInit(res){
                let self = this;
                self.listLoaded = true;
                self.goodsList = res.data.goods || [];

                //总数据量小于limit
                if(self.goodsList.length < self.limit) {
                    self.listCompleted = true;
                }
            },
            goToShop(shop){ //跳转店铺（大店铺shoptype 小店铺sid）
                shop = shop || {};
                if(shop.shoptype){ //大店铺shoptype
                    let _options = {
                        url: route.shop_index,
                        title: shop.sname || "商家店铺",
                        params: {
                            shopType: shop.shoptype
                        }
                    };
                    navigator.push(_options);
                } else {  //小店铺sid
                    let _options = {
                        url: route.shop_plus,
                        title: shop.sname || "店铺详情",
                        params: {
                            sid: shop.sid
                        }
                    };
                    navigator.push(_options);
                }
            }
        }
    }
</script>
